#include #include using namespace std; const int NUMBER_COUNT = 20; void initializeNumbers( int numbers[] ) { for(int i = 0; i < NUMBER_COUNT; i++) { numbers[i] = rand()%100 + 1; } } void displayNumbers( int numbers[] ) { for(int i = 0; i < NUMBER_COUNT; i++) { cout << numbers[i] << endl; } } void sortNumbers( int numbers[] ) { //outer loop - find the largest number this many times in the unsorted part of the list //sorted part of the list is the last i items for(int i = 0; i < NUMBER_COUNT-1; i++) { //find the largest item in the range 0 -> NUMBER_COUNT - 1 - i //switch the item that is the largest with the last item in the unsorted part of the list int indexOfLargest = 0; int indexOfLastItemInList = NUMBER_COUNT - 1 - i; for(int j = 1; j <= indexOfLastItemInList; j++) { if(numbers[j] > numbers[indexOfLargest]) { indexOfLargest = j; } } //swap the item at index indexOfLargest with the item at index indexOfLastItemInList int temp = numbers[indexOfLargest]; numbers[indexOfLargest] = numbers[indexOfLastItemInList]; numbers[indexOfLastItemInList] = temp; } } void main() { srand(time(NULL)); int numbers[NUMBER_COUNT]; initializeNumbers(numbers); displayNumbers(numbers); cout << endl << endl; sortNumbers(numbers); displayNumbers(numbers); }